package netnotifier.notification;

import java.util.Arrays;

import netnotifier.NetNotifier;

public class DNSPacket extends Packet
{
	public int tid=0;
	public int flags=0;

	public int numQuestions=0;
	public int numAnswers=0;
	public int numAuthorities=0;
	public int numAdditional=0;
	
	public String question=""; // derived data,
	public String type="";     // not actually part of header structure
	
	public static String[] defaultFields= new String[]{"type","question","numQuestions", "numAnswers","numAuthorities","numAdditional"};
	
	public class DNSRecord
	{
		public String recordName;
		public int recordType;
		public int recordClass;
	}
	
	public DNSPacket(byte[] rawData, int dnsOffset, int dnsLength)
	{
		// get the basic headers
		
		tid|=(rawData[dnsOffset+0]&0xff);
		tid<<=8;
		tid|=(rawData[dnsOffset+1]&0xff);
		
		flags|=(rawData[dnsOffset+2]&0xff);
		flags<<=8;
		flags|=(rawData[dnsOffset+3]&0xff);
		
		numQuestions|=(rawData[dnsOffset+4]&0xff);
		numQuestions<<=8;
		numQuestions|=(rawData[dnsOffset+5]&0xff);
		
		numAnswers|=(rawData[dnsOffset+6]&0xff);
		numAnswers<<=8;
		numAnswers|=(rawData[dnsOffset+7]&0xff);
		
		numAuthorities|=(rawData[dnsOffset+8]&0xff);
		numAuthorities<<=8;
		numAuthorities|=(rawData[dnsOffset+9]&0xff);
		
		numAdditional|=(rawData[dnsOffset+10]&0xff);
		numAdditional<<=8;
		numAdditional|=(rawData[dnsOffset+11]&0xff);
		
		if((flags&32768)==0)type="request";
		else type="response";
		
		if(numQuestions>0)
		{
			int strLen=0;
			
			// look for the null terminator up to 30 chars out
			for(int x=0;x<30;++x)
				if(rawData[dnsOffset+13+x]==0x00)
				{
					strLen=x;
					break;
				}
			
			question=new String(rawData,dnsOffset+13,strLen);
			
			// if anyone knows why I have do do this, I would like to know
			question=question.replace((char)0x01,'.');
			question=question.replace((char)0x02,'.');
			question=question.replace((char)0x03,'.');
			question=question.replace((char)0x04,'.');
			question=question.replace((char)0x07,'.');
		}
		
		// get the records (maybe later)
	}
}
